package mainFragment.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import java.util.ArrayList;

import mainFragment.bean.ZhaoShengSituationCoverBean;
import utils.MyLogUtils;

/**
 * Created by Administrator on 2017/3/16.
 */

public class ZhaoShengSituationCoverAdapter extends BaseAdapter {
    private ArrayList<ZhaoShengSituationCoverBean> arrayList;
    private Context context;

    private static final int TYPE_CATEGORY_ITEM = 0;
    private static final int TYPE_ITEM = 1;

    private static final String TAG = "ZhaoShengSituationCover";

    public ZhaoShengSituationCoverAdapter(Context context, ArrayList<ZhaoShengSituationCoverBean> arrayList) {
        this.arrayList = arrayList;
        this.context = context;
    }

    @Override
    public int getCount() {
        int count = 0;
        if (null != arrayList) {
            //  所有分类中item的总和是ListVIew  Item的总个数
            for (ZhaoShengSituationCoverBean zhaoShengSituationCoverBean : arrayList) {
                count += zhaoShengSituationCoverBean.getItemCount();
            }
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        int categroyFirstIndex = 0;
        for (ZhaoShengSituationCoverBean zhaoShengSituationCoverBean : arrayList) {
            int size = zhaoShengSituationCoverBean.getItemCount();
            // 在当前分类中的索引值
            int categoryIndex = position - categroyFirstIndex;
            // item在当前分类内
            if (categoryIndex < size) {
                if (zhaoShengSituationCoverBean.getItem(categoryIndex) != null) {
                    return zhaoShengSituationCoverBean.getItem(categoryIndex);
                } else {
                    return zhaoShengSituationCoverBean.getType();
                }
            }
            // 索引移动到当前分类结尾，即下一个分类第一个元素索引
            categroyFirstIndex += size;
        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        // 异常情况处理

        int categroyFirstIndex = 0;

        for (ZhaoShengSituationCoverBean category : arrayList) {
            int size = category.getItemCount();
            // 在当前分类中的索引值
            int categoryIndex = position - categroyFirstIndex;
            if (categoryIndex == 0) {
                return TYPE_CATEGORY_ITEM;
            }
            categroyFirstIndex += size;
        }

        return TYPE_ITEM;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case TYPE_CATEGORY_ITEM:
                MyTypeHolder myTypeHolder;
                if (null == convertView) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.zhaoshengsituation_cover_type, null);
                    myTypeHolder = new MyTypeHolder();
                    myTypeHolder.type = (TextView) convertView.findViewById(R.id.zhaoshengsituation_cover_item_type_tv);
                    convertView.setTag(myTypeHolder);
                }
                else{
                    myTypeHolder = (MyTypeHolder) convertView.getTag();
                }
                String itemValue = (String) getItem(position);
                myTypeHolder.type.setText(itemValue);
                MyLogUtils.e(TAG,position+"");
                if(itemValue.contains("理科")){
                    int color = context.getResources().getColor(R.color.red);
                    myTypeHolder.type.setTextColor(color);
                }
                else{
                    int color = context.getResources().getColor(R.color.common_color);
                    myTypeHolder.type.setTextColor(color);
                }
                break;

            case TYPE_ITEM:
                MyHolder myHolder;
                if (convertView == null) {
                    myHolder = new MyHolder();
                    convertView = LayoutInflater.from(context).inflate(R.layout.zhaoshengsituation_cover_item, null);
                    myHolder.count = (TextView) convertView.findViewById(R.id.zhaoshengsituation_cover_item_count);
                    myHolder.year = (TextView) convertView.findViewById(R.id.zhaoshengsituation_cover_item_year);
                    myHolder.plan = (TextView) convertView.findViewById(R.id.zhaoshengsituation_cover_item_plan);
                    myHolder.provline = (TextView) convertView.findViewById(R.id.zhaoshengsituation_cover_item_provinceline);
                    myHolder.rank = (TextView) convertView.findViewById(R.id.zhaoshengsituation_cover_item_rank);
                    myHolder.send = (TextView) convertView.findViewById(R.id.zhaoshengsituation_cover_item_send);
                    convertView.setTag(myHolder);
                } else {
                    myHolder = (MyHolder) convertView.getTag();
                }
                //内容视图
                myHolder.rank.setText(((ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean) getItem(position)).getRank());
                myHolder.count.setText(((ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean) getItem(position)).getCount());
                myHolder.provline.setText(((ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean) getItem(position)).getProvline());
                myHolder.send.setText(((ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean) getItem(position)).getSend());
                myHolder.plan.setText(((ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean) getItem(position)).getPlan());
                myHolder.year.setText(((ZhaoShengSituationCoverBean.ZhaoShengSituationCoverbean) getItem(position)).getYear());
                break;
        }
        return convertView;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return getItemViewType(position) != TYPE_CATEGORY_ITEM;
    }
    class MyTypeHolder{
        TextView type;
    }

    class MyHolder {
        TextView year;
        TextView plan;
        TextView send;
        TextView count;
        TextView provline;
        TextView rank;
    }
}
